import { getUserInfo } from '@/api/users'
import { ref } from 'vue'

/**
 * 用户详情服务
 */
export default function () {
  const getUserDetailLoading = ref(false)
  const userDetail = ref<CustomerDetail>({
    account: '',
    address: '',
    area: '',
    avatar: '',
    blacklist: false,
    city: '',
    code: '',
    createTime: '',
    email: '',
    id: '',
    lastLoginTime: '',
    level: '',
    levelId: '',
    money: 0,
    name: '',
    nickname: '',
    orderNo: '',
    phone: '',
    productNo: '',
    province: '',
    selectKnowledgeAuthor: false,
    status: 0,
    town: '',
    updateTime: '',
    userType: '',
    userTypeId: '',
    creditLimit: 0,
    settlementDeadline: 0,
    accountsReceivable: 0,
    creditLimitType: 1,
  })

  const getUserDetail = async (userId: string) => {
    try {
      getUserDetailLoading.value = true
      const res = await getUserInfo(userId)
      getUserDetailLoading.value = false
      if (res.data.code === 20000) {
        userDetail.value = res.data.data.row
        return ''
      }

      return res.data.message
    } catch (error) {
      getUserDetailLoading.value = false
      return '获取用户详情失败，请稍后重试'
    }
  }

  return {
    userDetail,
    getUserDetailLoading,
    getUserDetail,
  }
}
